import { Calculate } from './calculate';

export class LookupHelper {
  /**
   * lookup值数组转值
   * @param val
   */
  static Parse(val: Array<number>) {
    let result = 0;
    for (let i = 0; i < val.length; i++) {
      result = Calculate.OR(result, val[i]);
    }
    return result;
  }

  /**
   * lookup值转值数组
   * @param longValue
   */
  static RenderArray(longValue: number) {
    if (!longValue) {
      return [];
    }
    const result: Array<number> = [];
    for (let i = 1; i <= longValue; i = i * 2) {
      if (Calculate.AND(longValue, i) === i) {
        result.push(i);
      }
    }
    return result;
  }

  /**
   * 根据关系和值 转为显示的文字
   * @param relations
   * @param value
   * @param splitStr
   * @constructor
   */
  static ValueToTextByRelation(relations, value, splitStr = ',') {
    let showText = '';
    if (Array.isArray(relations)) {
      const match = [];
      const allValues = this.RenderArray(value);
      relations.map((relation) => {
        if (allValues.includes(relation.Code)) {
          match.push(relation.Name);
        }
      });
      if (match.length > 0) {
        showText = match.join(` ${splitStr} `);
      }
    }
    return showText;
  }
}
